package com.ruoyi.web.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.message.domain.MessageUser;
import com.ruoyi.web.message.mapper.MessageUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.message.mapper.MessageMapper;
import com.ruoyi.web.message.domain.Message;
import com.ruoyi.web.message.service.IMessageService;

/**
 * 消息Service业务层处理
 *
 * @author 范书铭
 * @date 2024-10-11
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private MessageUserMapper messageUserMapper;

    /**
     * 查询消息列表
     *
     * @param message 消息
     * @return 消息
     */
    @Override
    public List<Message> selectMessageList(Message message) {
        return messageMapper.selectMessageList(message);
    }

    @Override
    public Message getMessageById(Long id, Long userId) {
        Message message = getById(id);
        MessageUser messageUser = messageUserMapper.selectOne(new LambdaQueryWrapper<MessageUser>()
                .eq(MessageUser::getUserId,userId)
                .eq(MessageUser::getMessageId,message.getId()));
        if(messageUser!=null) {
            messageUser.setIsRead(true);
            messageUserMapper.update(messageUser, null);
        }
        return message;
    }
}